package cn.tedu.game2;

import java.awt.*;
import java.util.Random;

//创建一个小球，Ball类型，属性，color x,y,length,speed,··,moVe()仅需实现忘右下角运动
public class Ball {
    int x;
    int y;
    Color color;
    int length;
    int speed;

    int state=0;

    public Ball(){
        Random random =  new Random();
        this.speed = random.nextInt(10)+2;
        this.length = random.nextInt(100)+20;
        this.x = random.nextInt(1920);
        this.y = random.nextInt(1080);
        int red = random.nextInt(256);
        int green = random.nextInt(256);
        int blue  = random.nextInt(256);
        this.color = new Color(red,green,blue);
    }

    public void move(){

        //根据状态修改坐标
        if(this.state==0){
            y = y - speed;
            x = x - speed;
        }
        if(this.state==1){
            this.y = y - speed;
            this.x = x + speed;
        }
        if(this.state==2){
            this.y = y + speed;
            this.x = x + speed;
        }
        if(this.state==3){
            this.y = y + speed;
            this.x = x - speed;
        }
        //撞到上边界
        if (this.y<0){
            if(this.state==0){this.state=3;}
            if(this.state==1){this.state=2;}
        }
        //撞到下边界
        if (this.y+length>1080){
            if(this.state==2){this.state=1;}
            if(this.state==3){this.state=0;}
        }
        //撞到左边界
        if (this.x<0){
            if(this.state==0){this.state=1;}
            if(this.state==3){this.state=2;}
        }
        //撞到右边界
        if (this.x+length >1920){
            if(this.state==2){this.state=3;}
            if(this.state==1){this.state=0;}
        }

        //撞到边界，根据之前的状态改编运动轨迹
    }
    public void hitBrick(Brick brick){
        if(this.x+length/2>=brick.x&&this.x+length/2<= brick.x+ brick.width&&this.y >= brick.y && this.y <= brick.y + brick.height){
            if (brick.height>0) { brick.height-=5; }
            if(this.state==0){this.state=3;}
            if(this.state==1){this.state=2;}
        }

    }
}
